package com.efast.cafe.portal.service.api.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.json.JSONArray;
import com.efast.cafe.framework.json.JSONException;
import com.efast.cafe.framework.json.JSONObject;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.StringUtil;
import com.efast.cafe.portal.bean.api.PortalApiViewBean;
import com.efast.cafe.portal.bean.api.PortalApiViewQueryBean;
import com.efast.cafe.portal.dao.api.PortalApiViewAuthDao;
import com.efast.cafe.portal.dao.api.PortalApiViewDao;
import com.efast.cafe.portal.entity.api.PortalApiView;
import com.efast.cafe.portal.entity.api.PortalApiViewAuth;
import com.efast.cafe.portal.service.api.IPortalApiViewService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

@Service
@Transactional
@RestController
public class PortalApiViewServiceImpl implements IPortalApiViewService {

	@Autowired
	private PortalApiViewDao portalApiViewDao;
	@Autowired
	public StringRedisTemplate stringRedisTemplate;

	@Override
	public void insertApiView(PortalApiViewBean portalApiView) {
		if(StringUtil.isNullOrBlank(portalApiView.getViewCode())){
			throw new ServiceException("viewcode 不能为空");
		}
		List<PortalApiView> list = portalApiViewDao.checkUniqueViewCode(portalApiView);
		if (!list.isEmpty()) {
			throw new ServiceException("viewcode 重复请重新输入");
		}
		if(StringUtil.isNullOrBlank(portalApiView.getViewDisplay())){
			portalApiView.setViewDisplay(null);
		}
		
		stringRedisTemplate.opsForValue().set(CommonConstant.SESSION_VIEW_VER, UUID.randomUUID().toString());
		// 1.新增vies主表
		portalApiViewDao.saveView(portalApiView);

	}

	@Override
	public void updateApiView(PortalApiView portalApiView) {

		// 修改view
		portalApiViewDao.updateView(portalApiView);
		stringRedisTemplate.opsForValue().set(CommonConstant.SESSION_VIEW_VER, UUID.randomUUID().toString());

	}

	@Override
	public void deleteApiView(PortalApiView portalApiView) {
		portalApiViewDao.deleteView(portalApiView.getId());
		stringRedisTemplate.opsForValue().set(CommonConstant.SESSION_VIEW_VER, UUID.randomUUID().toString());
	}

	@Override
	public PortalApiView queryApiViewByViewId(PortalApiView portalApiView) {
		portalApiView = portalApiViewDao.queryById(portalApiView.getId());
		return portalApiView;
	}

	@Override
	public PortalApiViewQueryBean queryViewByModuleId(PortalApiViewQueryBean queryBean) {
		queryBean.setResultList(portalApiViewDao.queryViewByModuleIdList(queryBean));
		queryBean.setLength(portalApiViewDao.queryViewByModuleIdCount(queryBean));
		return queryBean;
	}

	/**
	 * 校验viewCode是否循环引用
	 * 
	 * @param viewCode
	 * @param componmetCode
	 * @return
	 */
	@Override
	public boolean checkComponentView(String viewCode, String componmetCode) {
		List<PortalApiView> list = portalApiViewDao.queryViewAll();

		Map<String, String> view = new HashMap<>();
		for (PortalApiView portalApiView : list) {
			view.put(portalApiView.getViewCode(), portalApiView.getComponent());
		}
		
		if(!view.containsKey(componmetCode)){
			throw new ServiceException("该view :"+componmetCode+" 不存在");
		}
		return checkViewLoop(componmetCode,view,viewCode);
	}
	

	private  boolean checkViewLoop(String componmetCode, Map<String, String> view, String viewCode) {
		String childrenView=view.get(componmetCode);
		try {
			JSONArray  arrayJson=new JSONArray(childrenView);
			for (int i = 0; i < arrayJson.length(); i++) {
				JSONObject obj=	arrayJson.getJSONObject(i);
			String type= (String) obj.get("compType");
			String code= (String) obj.get("compName");
			if(("view").equals(type)){
			if(code.equals(viewCode)){
				return false;
			}else{
				if(!checkViewLoop(code,view,viewCode)){
					return false;
				}
			}
			}
			}
			
		} catch (JSONException e) {
			throw new ServiceException(componmetCode+": component解析失败");
		}
		return true;
	}

	@Autowired
	private PortalApiViewAuthDao portalApiViewAuthDao;
	
	@Override
	public Object queryByViewCode(String viewCode) {
		// TODO Auto-generated method stub
		List<PortalApiViewAuth> auths = portalApiViewAuthDao.queryAuthByViewCode(viewCode, SpringWebUtils.getRequestCompany(), SpringWebUtils.getRequestUser());
		 PortalApiViewBean bean = portalApiViewDao.queryByViewCode(viewCode,ConfigUtil.getValue("cafe.system.ver"), ConfigUtil.getValue("cafe.system.indentifier"));
		 if(null!=bean)
			 bean.setAuths(auths);
		 return bean;
	
	}
	
	

	@Override
	public List<PortalApiView> selectViewsListByMouduleId(Integer moduleId, String code, String i18nName) {
		
		return portalApiViewDao.selectViewByModuleId(moduleId,code,i18nName);
	}

	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param moduleId
	    * @return
	    * @see com.efast.cafe.portal.remote.api.PortalApiViewService#selectViewsListByMouduleId(java.lang.Integer)
	    */
	    
	@Override
	public List<PortalApiView> selectViewsListByMouduleId(Integer moduleId) {
		return portalApiViewDao.selectViewsListByMouduleId(moduleId);
	}

		
		    /* (非 Javadoc)
		    * 
		    * 
		    * @param viewCode
		    * @return
		    * @see com.efast.cafe.portal.remote.api.PortalApiViewService#selectViewByCode(java.lang.Integer)
		    */
		    
		@Override
		public PortalApiViewBean selectViewByCode(String viewCode) {
			PortalApiViewBean bean = portalApiViewDao.queryByViewCode(viewCode,ConfigUtil.getValue("cafe.system.ver"), ConfigUtil.getValue("cafe.system.indentifier"));
			 
			return bean;
		}
	
}
